import BaseEvent from './BaseEvent.js';
/**
* @description 链接类
*/
export default class MQC extends BaseEvent{
    constructor({
        MQIP = "",
        MQPort = 61614,
        subscribe = [],
    } = {}){
        super();
        //MQ连接的ID
        this.clientID;
        //连接配置
        this.config = {
            MQIP,MQPort,subscribe
        };
        this.MQclient = {};//MQ对象
        this.eve = {};//事件集合
        this.eves = {};
        //连接失败事件,连接成功事件,失去连接事件,连接开始之前事件
        ["clientFail","cliented","clientlost","beforclient"].forEach(e=>{
            this.eves[e] = e;
        })
        this.showconsole = true;//是否打开日志
        this.cliented = false;//是否正在链接
        this.init();//初始
    }
    //初始方式
    init(){
        const t = this;
        t.clientID = new Date().getTime().toString();
        const {MQIP,MQPort} = t.config;
        try{
            t.MQclient = new Messaging.Client(MQIP, MQPort, t.clientID);
            //监听事件
            t.MQclient.onMessageArrived = msg => {
                let topic = msg.destinationName;
                msg = eval('('+msg.payloadString+')');
                t.showconsole && console.log(`返回消息${topic}${JSON.stringify(msg)}${new Date().getTime().toString()}`)
                t.trigger(topic,[msg,t]);
            };
            //失去连接
            t.MQclient.onConnectionLost = responseObject => {
                if (responseObject.errorCode !== 0) {
                    t.cliented = false;
                    t.showconsole && console.log(`失去连接:${responseObject.errorMessage}`);
                    t.trigger(t.eves.clientlost,[responseObject.errorMessage,t]);
                }
            }
        }catch(ex){}
    }
    //链接方法
    client(){
        const t = this;
        t.trigger(t.eves.beforclient,[t.MQclient,t]);
        const onSuccess = () => {
            t.cliented = true;
            t.showconsole && console.log(`连接成功:${t.config.MQIP}`);
            t.trigger(t.eves.cliented,[t.MQclient,t]);
            //订阅主题
            t.config.subscribe.forEach((item)=>{
                t.MQclient.subscribe(item);
            })
        };
        const onFailure = () => {
            t.cliented = false;
            t.showconsole && console.log(`连接失败:${t.config.MQIP}`);
            t.trigger(t.eves.clientFail,[t.MQclient,t]);
        };

        //链接配置
        t.MQclient.connect({onSuccess,onFailure});
        return t;
    }
    //断开方法
    disconnect(){
        const t = this;
        t.MQclient.disconnect();
    }
    /**
     * 订阅主题
     * @param string name 主题名
     */
    subscribe(name){
        const t = this;
        t.showconsole && console.log(`开始订阅主题:${name}`);
        t.MQclient.subscribe(name);
        return t;
    }
    /**
     * 推送消息主题
     * @param string name 主题名
     * @param string msg 信息
     */
    publish(name,msg){
        const t = this;
        if(typeof msg !== "string") msg = JSON.stringify(msg);
        t.showconsole && console.log(`推送主题:${name};消息为:${msg}`);
        const message = new Messaging.Message(msg);
        message.destinationName = name;
        t.MQclient.send(message);
        return t;
    }
}